package com.guigu.qingcheng.service_user.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.guigu.qingcheng.pojo.user.TbProvinces;
import com.guigu.qingcheng.service_user.service.TbProvincesService;
import com.guigu.qingcheng.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 省份信息表 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2020-07-08
 */
@RestController
@RequestMapping("/service_user/tb-provinces")
public class TbProvincesController {

    @Autowired
    private TbProvincesService provincesService;

    @PostMapping("/provinces/findPage")
    public R findPage(@RequestParam("page") Integer page, @RequestParam("size") Integer size, @RequestBody(required = false) Map<String,Object> searchMap){
        Page<TbProvinces> page1=provincesService.findPage(searchMap, page, size);
        long total=page1.getTotal();
        List<TbProvinces> list=page1.getRecords();
        if(list.size()>0){
            return R.ok().data("total",total).data("rows",list);
        }
        return R.ok().data("list",page1);
    }

    /**
     * 根据ID查询数据
     * @param provinceid
     * @return
     */
    @GetMapping("/provinces/findById")
    public R findById(@RequestParam("provinceid") String provinceid){
        TbProvinces provinces=provincesService.getById(provinceid);
        if(provinces==null){
            return R.error().data("provinces",provinces);
        }
        return R.ok().data("provinces",provinces);
    }

    /**
     * 增加数据
     * @param provinces
     * @return
     */
    @PostMapping("/provinces/add")
    public R add(@RequestBody TbProvinces provinces){
        boolean save = provincesService.save(provinces);
        if(save) {
            return R.ok();
        } else {
            return R.error();
        }
    }

    /**
     * 修改数据
     * @param provinces
     * @return
     */
    @PostMapping("/provinces/update")
    public R update(@RequestBody TbProvinces provinces){
        boolean flag = provincesService.updateById(provinces);
        if(flag) {
            return R.ok();
        } else {
            return R.error();
        }
    }

    /**
     * 删除数据
     * @param provinceid
     * @return
     */
    @GetMapping("/provinces/delete")
    public R delete(@RequestParam("provinceid") String provinceid){
        boolean flag=provincesService.removeById(provinceid);
        if(flag){
            return R.ok();
        }else {
            return R.error();
        }
    }





}

